package com.wangxiaonan.process.plugin.method;

import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import java.util.List;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * 字符串 判空方法<br>
 * String ""        ==>    ""
 * String null      ==>    ""
 * String "abc"     ==>    "abc"
 * String " abc "   ==>    "abc"
 *
 * @Author: wangxiaonan
 * @Date: 2019/2/28
 **/
public class BlankMethod implements TemplateMethodModelEx {

    @Override
    public Object exec(List arguments) throws TemplateModelException {
        String result;
        if (CollectionUtils.isEmpty(arguments)) {
            result = "";
        } else if (arguments.size() > 1) {
            throw new IllegalArgumentException("参数个数不能多于1");
        } else if (arguments.get(0) == null) {
            result = "";
        } else {
            result = StringUtils.trimWhitespace(arguments.get(0).toString());
        }
        System.out.println("O(∩_∩)O哈！" + result + "~\\(≧▽≦)/~");
        return result;
    }
}
